/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.Remuneraciones.controller;


import com.Remuneraciones.model.Parametros;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 18472014-0
 */
public class Calculo extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Calculo</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Calculo at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       List errorMsg = new ArrayList();
       
       try{
           String DT=request.getParameter("DT");
            String SB=request.getParameter("SB");
            String HE=request.getParameter("HE");
             String CF=request.getParameter("CF");
            String CIUDAD=request.getParameter("ciudades");
             
            if (( CIUDAD==null)||(CIUDAD.trim().length()==0)){
                
                CIUDAD=request.getParameter("genre");
            }
            
            if (( DT==null)||(DT.trim().length()==0)){
                
                errorMsg.add("Debe ingresar LOS DATOS");
            }
            
             if (( SB==null)||(SB.trim().length()==0)){
                
                errorMsg.add("Debe ingresar un SUELDO");
            } else if (!SB.trim().matches("\\d\\d\\d\\d\\d\\d")){
                   errorMsg.add("Debe ingresar un SUELDO en numeros"); 
            
            }
             
             if (( HE==null)||(HE.trim().length()==0)){
                
                errorMsg.add("Debe ingresar LAS HE");
            } else if (!HE.trim().matches("\\d\\d\\d\\d")){
                   errorMsg.add("Debe ingresar LAS HE en numeros"); 
            
            }
             
             if (( CF==null)||(CF.trim().length()==0)){
                
                errorMsg.add("Debe ingresar LAS CF");
            } else if (!CF.trim().matches("\\d")){
                   errorMsg.add("Debe ingresar LAS CF en numeros"); 
            
            }
             if (!errorMsg.isEmpty()){
            request.setAttribute("error", errorMsg);
            RequestDispatcher view=request.getRequestDispatcher("/Parametros.view");
            view.forward(request, response);
            return;
            }
         Parametros parameter = new Parametros(DT,SB,HE,CF);     
       
         
         request.setAttribute("clase",parameter);
            
            RequestDispatcher view= request.getRequestDispatcher("/Success.view");
            view.forward(request, response);
            
            
            
        }
        
        catch (RuntimeException e) {
            
            errorMsg.add("ERROR: SUPER ERROR FATAL" +e.getMessage());
            request.setAttribute("error", errorMsg);
            RequestDispatcher view=request.getRequestDispatcher("/Parametros.view");
            view.forward(request, response);
            
        }
       }
    

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
